//
//  LYFSubscribeTableViewCell.m
//  段子手
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LYFSubscribeTableViewCell.h"

@interface LYFSubscribeTableViewCell ()

@property (nonatomic, retain) UIImageView *imgView;/**< 左边图片 */
@property (nonatomic, retain) UILabel *titleLabel;/**< 上面标题 */
@property (nonatomic, retain) UILabel *smallLabel;/**< 中间内容文字 */
@property (nonatomic, retain) UILabel *subscriptionLabel;/**< 订阅label */
@property (nonatomic, retain) UILabel *totalPostsLabel;/**< 总帖数label */
@property (nonatomic, retain) UIButton *cancelSubscriptionButton;/**< 取消订阅button */

@end

@implementation LYFSubscribeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)setModel:(LYFDiscoverModel *)model {
    _model = model;
    [_imgView sd_setImageWithURL:[NSURL URLWithString:_model.icon_url]];
    _titleLabel.text = _model.name;
    
    _smallLabel.text = _model.placeholder;
    
    
    _subscriptionLabel.text = [NSString stringWithFormat:@"%ld订阅", _model.subscribe_count];
    _totalPostsLabel.text = [NSString stringWithFormat:@"总帖数%ld", _model.total_updates];
    _totalPostsLabel.textColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    
}

- (void)createCell {
    self.imgView = [[UIImageView alloc] init];
    [self.contentView addSubview:_imgView];
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_titleLabel];
    
    self.smallLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_smallLabel];
    
    self.subscriptionLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_subscriptionLabel];
    
    self.totalPostsLabel = [[UILabel alloc] init];
    [self.contentView addSubview:_totalPostsLabel];
    
    self.cancelSubscriptionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_cancelSubscriptionButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _imgView.frame = CGRectMake(10, 10, 80, 80);
        _imgView.backgroundColor = [UIColor yellowColor];
    
    _titleLabel.frame = CGRectMake(100, 10, SCREEN_WIDTH / 2, 30);
    _titleLabel.font = [UIFont systemFontOfSize:15];
    //    _titleLabel.backgroundColor = [UIColor greenColor];
    
    _smallLabel.frame = CGRectMake(100, 40, SCREEN_WIDTH - 200, 25);
    _smallLabel.font = [UIFont systemFontOfSize:13];
    _smallLabel.textColor = [UIColor grayColor];
    //    _smallLabel.backgroundColor = [UIColor grayColor];
    
    _subscriptionLabel.frame = CGRectMake(100, 65, 100, 25);
    _subscriptionLabel.font = [UIFont systemFontOfSize:13];
    _subscriptionLabel.textColor = [UIColor grayColor];
    //    _subscriptionLabel.backgroundColor = [UIColor purpleColor];
    
    _totalPostsLabel.frame = CGRectMake(200, 65, 100, 25);
    _totalPostsLabel.font = [UIFont systemFontOfSize:13];
    //    _totalPostsLabel.textColor = [UIColor grayColor];
    //    _totalPostsLabel.backgroundColor = [UIColor orangeColor];
    
    _cancelSubscriptionButton.frame = CGRectMake(SCREEN_WIDTH - 75, 40, 70, 20);
    _cancelSubscriptionButton.backgroundColor = [UIColor grayColor];
    _cancelSubscriptionButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _cancelSubscriptionButton.layer.cornerRadius = 5;
    [_cancelSubscriptionButton setTitle:@"取消订阅" forState:UIControlStateNormal];
    [_cancelSubscriptionButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
}


/** 取消订阅button点击方法 */
- (void)buttonAction:(UIButton *) button {
    
    [[LYFDataBaseHandlle sharedDataBaseHandle] openDB];
    
    
    [JCAlertView showTwoButtonsWithTitle:@"提示" Message:@"是否取消订阅" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{

        [[LYFDataBaseHandlle sharedDataBaseHandle] deleteModel:_model];
        
        [self.delegate reloadTableView:_model];
    } ButtonType:JCAlertViewButtonTypeCancel ButtonTitle:@"取消" Click:^{

    }];
    
    
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
